<?php header('Content-type: text/xml; charset=UTF-8', true); ?>
<?php
$bbxml = 'backup-20100111193850.xml'; // 这里是blogbus备份文件的路径

// 先输出一些必要的东东
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>

<rss version="2.0"
	xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:wp="http://wordpress.org/export/1.0/"
>

<channel>
	<title>Test</title>
	<link>http://localhost/wordpress</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Mon, 11 Jan 2010 11:03:24 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<wp:wxr_version>1.0</wp:wxr_version>
	<wp:base_site_url>http://localhost/wordpress</wp:base_site_url>
	<wp:base_blog_url>http://localhost/wordpress</wp:base_blog_url>
<?php
set_magic_quotes_runtime(0);
$datalines = file($bbxml);
$data = implode('', $datalines); // squish it
unset ($datalines);
$data = str_replace(array ("\r\n", "\r"), "\n", $data);

preg_match_all('|<Sort>(.*?)</Sort>|is', $data, $tags);
unset($tags[0]);
$tags = array_unique($tags[1]);
$tags = array_filter($tags);
foreach ($tags as $tag) {
    $tag = trim($tag);
    echo '<wp:category><wp:category_nicename>'.$tag.'</wp:category_nicename><wp:category_parent></wp:category_parent><wp:cat_name><![CDATA['.$tag.']]></wp:cat_name></wp:category>'."\n";
}
unset($tags);
unset($tag);

preg_match_all('|<Tags>(.*?)</Tags>|is', $data, $cats);
unset($cats[0]);
$cats = array_unique($cats[1]);
$cats = array_filter($cats);
foreach ($cats as $cat) {
    $cat_cache = explode(' ', $cat);
    foreach ($cat_cache as $c) {
        $category[] = $c;
    }
}

unset($cat);unset($cats);unset($c);
foreach ($category as $c) {
    $c = trim($c);
    echo '<wp:tag><wp:tag_slug>'.$c.'</wp:tag_slug><wp:tag_name><![CDATA['.$c.']]></wp:tag_name></wp:tag>'."\n";
}
unset($category);unset($c);

preg_match_all('|<Log>(.*?)</Log>|is', $data, $logs);
$logs = $logs[1];
foreach ($logs as $log) {
    preg_match('|<Title>(.*?)</Title>|is', $log, $title);
    $title = $title[1];
    preg_match('|<Sort>(.*?)</Sort>|is', $log, $cat);
    if ('' != $cat[1]) {
        $cats_tags = '<category><![CDATA['.$cat[1].']]></category>'."\n";
        $cats_tags .= '<category domain="category" nicename="'.$cat[1].'"><![CDATA['.$cat[1].']]></category>'."\n";
    }
    preg_match('|<Tags>(.*?)</Tags>|is', $log, $tags);
    $tag = explode(' ', $tags[1]);
    if (0 != count($tag)) {
        foreach ($tag as $t) {
            $cats_tags .= '<category domain="tag"><![CDATA['.$t.']]></category>'."\n";
            $cats_tags .= '<category domain="tag" nicename="'.$t.'"><![CDATA['.$t.']]></category>'."\n";
        }
    }

    preg_match('|<LogDate>(.*?)</LogDate>|is', $log, $date);
    $date = date('Y-m-d H:i:s', strtotime($date[1]));
    $gmt_date = date('Y-m-d H:i:s', strtotime($date)-28800);
    $pubdate = date('r', strtotime($gmt_date));

    preg_match('|<Content>(.*?)</Content>|is', $log, $content);
    $content = $content[1];

    echo $item_template = <<<IT
<item>
<title>$title</title>
<link>http://localhost/wordpress/?p=6</link>
<pubDate>$pubdate</pubDate>
<dc:creator><![CDATA[admin]]></dc:creator>
$cats_tags
<guid isPermaLink="false">http://localhost/wordpress/?p=6</guid>
<description></description>
<content:encoded>$content</content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id></wp:post_id>
<wp:post_date>$date</wp:post_date>
<wp:post_date_gmt>$gmt_date</wp:post_date_gmt>
<wp:comment_status>open</wp:comment_status>
<wp:ping_status>open</wp:ping_status>
<wp:post_name>$title</wp:post_name>
<wp:status>publish</wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type>post</wp:post_type>
<wp:post_password></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:postmeta>
<wp:meta_key>_edit_lock</wp:meta_key>
<wp:meta_value></wp:meta_value>
</wp:postmeta>
<wp:postmeta>
<wp:meta_key>_edit_last</wp:meta_key>
<wp:meta_value>1</wp:meta_value>
</wp:postmeta>
	</item>
IT;
    echo "\n";
}

echo <<<END
</channel>
</rss>
END;

/* */
/* */